package com.xingxing.Adapter;

import android.content.Context;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.xingxing.Activity.R;
import com.xingxing.Bean.Video;
import com.xingxing.View.CusVideoView;
import java.util.ArrayList;

public class DikDokAdapter extends RecyclerView.Adapter<DikDokAdapter.ViewHolder> {
    ArrayList<Video> videos;
    private Context mContext;

    public DikDokAdapter(Context context, String json) {
        this.mContext = context;
        videos = new Gson().fromJson(json, new TypeToken<ArrayList<Video>>() {
        }.getType());
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        return new ViewHolder(LayoutInflater.from(mContext).inflate(R.layout.item_dikdok_layout, viewGroup, false));
    }

    @Override
    public void onBindViewHolder(@NonNull final ViewHolder holder, int pos) {
        holder.mVideoView.setVideoURI(Uri.parse(videos.get(pos % videos.size()).getFileDirectory()));
        holder.mThumb.setImageBitmap(holder.mVideoView.getDrawingCache());//获取视频第一帧作为封面图片
        holder.mTitle.setText(videos.get(pos % videos.size()).getVideoTitle());
        holder.mMarquee.setText(videos.get(pos % videos.size()).getVideoText());
        holder.mMarquee.setSelected(true);
    }

    @Override
    public int getItemCount() {
        return Integer.MAX_VALUE;
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        RelativeLayout mRootView;
        ImageView mThumb;
        ImageView mPlay;
        TextView mTitle;
        TextView mMarquee;
        CusVideoView mVideoView;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            mRootView = itemView.findViewById(R.id.mRootView);
            mThumb = itemView.findViewById(R.id.mThumb);
            mPlay = itemView.findViewById(R.id.mPlay);
            mVideoView = itemView.findViewById(R.id.mVideoView);
            mTitle = itemView.findViewById(R.id.mTitle);
            mMarquee = itemView.findViewById(R.id.mMarquee);
        }
    }
}
